package com.service;

import com.alibaba.fastjson.JSON;
import com.entity.MenDian_xtl;
import com.entity.ShangPing_xtl;
import com.utils.EntityFactory;
import com.utils.ExecUtil;
import com.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/shangping_search"})
public class ShangPingService_xtl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getServletPath().equals("/shangping_search")){
            Result.fail(404);
            return;
        }
        List<ShangPing_xtl> list=new ArrayList<>();
        List<Object> params=new ArrayList<>();
        String sql="select * from shangping_xtl";
        boolean judge =true;
        if (!req.getParameter("shop").isEmpty()){
            params.add(req.getParameter("shop"));
            if (judge){
                judge=false;
                sql=sql+" where";
            }else {
                sql=sql+" and";
            }
            sql=sql+" shop like \"%\"?\"%\"";
        }else if(!req.getParameter("goods").isEmpty()){
            params.add(req.getParameter("goods"));
            if (judge){
                judge=false;
                sql=sql+" where";
            }else {
                sql=sql+" and";
            }
            sql=sql+" goods like \"%\"?\"%\"";
        } else if (!req.getParameter("time1").isEmpty()) {
            params.add(req.getParameter("time1"));
            if (judge){
                judge=false;
                sql=sql+" where";
            }else {
                sql=sql+" and";
            }
            sql=sql+" time >=?";
        }else if(!req.getParameter("time2").isEmpty()){
            params.add(req.getParameter("time2"));
            if (judge){
                judge=false;
                sql=sql+" where";
            }else {
                sql=sql+" and";
            }
            sql=sql+" time <=?";
        }else if(!req.getParameter("status").isEmpty()){
            params.add(req.getParameter("status"));
            if (judge){
                judge=false;
                sql=sql+" where";
            }else {
                sql=sql+" and";
            }
            sql=sql+" status=?";
        }
        try {
            ResultSet rt= (ResultSet) ExecUtil.execute(sql,params,req,false);
            while (rt.next()){
                EntityFactory entityFactory = new EntityFactory<>(ShangPing_xtl.class);
                ShangPing_xtl o = (ShangPing_xtl) entityFactory.startCombine(rt);
                list.add(o);
            }
            resp.getWriter().write(JSON.toJSONString(list));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
